﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommonLibrary.DataModel.Basic;
using System.Web.Mvc;
using System.Data;
using System.Workflow.Activities;

namespace OAonline.Models
{
    [ModelBinder(typeof(UserModelBinder))]
    public class UserModel
    {
        #region common
        
        public UserEntity user { set; get; }
        
        #endregion

        #region wf
        public List<SelectListItem> list_wftype { set; get; }
        public List<SelectListItem> list_form { set; get; }
        public List<SelectListItem> list_wftemplate { set; get; }

        public Dictionary<int, StateMachineWorkflowInstance> WorkflowStaticTemplate { set; get; }
        #endregion
    }

    public class UserModelBinder : IModelBinder
    {
        private const string CartSessionKey = "_usermodel";

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.Model != null)
                throw new InvalidOperationException("Cannot update instances");
            
            // Return the cart from Session[] (creating it first if necessary)
            UserModel user = (UserModel)controllerContext.HttpContext.Session[CartSessionKey];
            if (user == null)
            {
                user = new UserModel();
                controllerContext.HttpContext.Session[CartSessionKey] = user;
            }
            return user;
        }
    }
}
